//
//  MIMCModule.m
//  RNReactNativeMimc
//
//  Created by Ming on 2020/5/3.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "MIMCModule.h"


@implementation MIMCModule

NSString *onHandleMIMCMessage = @"onHandleMIMCMessage";
NSString *onHandleMIMCGroupMessage = @"onHandleMIMCGroupMessage";
NSString *onHandleMIMCOnlineMessage = @"onHandleMIMCOnlineMessage";
NSString *onHandleMIMCUnlimitedGroupMessage = @"onHandleMIMCUnlimitedGroupMessage";
NSString *onHandleMIMCServerAck = @"onHandleMIMCServerAck";
NSString *onHandleMIMCOnlineMessageAck = @"onHandleMIMCOnlineMessageAck";
NSString *onHandleMIMCSendMessageTimeout = @"onHandleMIMCSendMessageTimeout";
NSString *onHandleMIMCSendGroupMessageTimeout = @"onHandleMIMCSendGroupMessageTimeout";
NSString *onHandleMIMCSendUnlimitedGroupMessageTimeout = @"onHandleMIMCSendUnlimitedGroupMessageTimeout";
NSString *onHandleMIMCStatusChanged = @"onHandleMIMCStatusChanged";
NSString *onHandleMIMCCreateUnlimitedGroup = @"onHandleMIMCCreateUnlimitedGroup";
NSString *onHandleMIMCJoinUnlimitedGroup = @"onHandleMIMCJoinUnlimitedGroup";
NSString *onHandleMIMCQuitUnlimitedGroup = @"onHandleMIMCQuitUnlimitedGroup";
NSString *onHandleMIMCDismissUnlimitedGroup = @"onHandleMIMCDismissUnlimitedGroup";


+ (BOOL)requiresMainQueueSetup {
    return true;
}

// 初始化user管理
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.userManager = [XMUserManager sharedInstance];
        self.userManager.showRecvMsgDelegate = self;
        self.userManager.returnUserStatusDelegate = self;
        self.userManager.onHandleUnlimitedGroupDelegate = self;
    }
    return self;
}

- (NSArray<NSString *> *)supportedEvents {
    return @[
        onHandleMIMCMessage,
        onHandleMIMCGroupMessage,
        onHandleMIMCGroupMessage,
        onHandleMIMCUnlimitedGroupMessage,
        onHandleMIMCServerAck,
        onHandleMIMCOnlineMessageAck,
        onHandleMIMCSendMessageTimeout,
        onHandleMIMCSendGroupMessageTimeout,
        onHandleMIMCSendUnlimitedGroupMessageTimeout,
        onHandleMIMCStatusChanged,
        onHandleMIMCCreateUnlimitedGroup,
        onHandleMIMCJoinUnlimitedGroup,
        onHandleMIMCQuitUnlimitedGroup,
        onHandleMIMCDismissUnlimitedGroup
    ];
}

RCT_EXPORT_MODULE()

// 设置获取到的登录token
RCT_EXPORT_METHOD(initToken:(NSString *) token){
    [self.userManager setToken: token];
}

//login
RCT_EXPORT_METHOD(login:(NSString *)appId appAccount:(NSString *)appAccount){
    [self.userManager userLogin:[appId longLongValue] appAccount:appAccount];
}

//logout
RCT_EXPORT_METHOD(logout){
    [self.userManager userLogout];
}


//发送私聊
RCT_EXPORT_METHOD(sendMessage:(NSDictionary *)params){
    NSString *toAccount = [params objectForKey:@"toAccount"];
    NSString *payload = [params objectForKey:@"payload"];
    NSString *bizType = [params objectForKey:@"bizType"];
    [self.userManager sendMessage:toAccount payload:[payload dataUsingEncoding:NSUTF8StringEncoding] bizType:bizType];
}

//发送群聊
RCT_EXPORT_METHOD(sendGroupMessage:(NSDictionary *)params){
    NSString *topicId = [params objectForKey:@"topicId"];
    NSString *payload = [params objectForKey:@"payload"];
    NSString *bizType = [params objectForKey:@"bizType"];
    [self.userManager sendGroupMessage:[topicId longLongValue] payload:[payload dataUsingEncoding:NSUTF8StringEncoding] bizType:bizType];
}

//发送在线消息
RCT_EXPORT_METHOD(sendOnlineMessage:(NSDictionary *)params){
    NSString *toAccount = [params objectForKey:@"toAccount"];
    NSString *payload = [params objectForKey:@"payload"];
    NSString *bizType = [params objectForKey:@"bizType"];
    [self.userManager sendOnlineMessage:toAccount payload:[payload dataUsingEncoding:NSUTF8StringEncoding] bizType:bizType];
}

//发送无限大群消息
RCT_EXPORT_METHOD(sendUnlimitedGroupMessage:(NSDictionary *)params){
    NSString *topicId = [params objectForKey:@"topicId"];
    NSString *payload = [params objectForKey:@"payload"];
    NSString *bizType = [params objectForKey:@"bizType"];
    [self.userManager sendUnlimitedGroupMessage:[topicId longLongValue] payload:[payload dataUsingEncoding:NSUTF8StringEncoding] bizType:bizType];
}

//处理群聊消息
- (void)onHandleGroupMessage:(NSArray<MIMCGroupMessage *> *)packets {
    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (MIMCGroupMessage *msg in packets) {
        NSMutableDictionary *groupMessage = [[NSMutableDictionary alloc] init];
        groupMessage[@"packetId"] = [msg getPacketId];
        groupMessage[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
        groupMessage[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
        groupMessage[@"fromAccount"] = [msg getFromAccount];
        groupMessage[@"fromResource"] = [msg getFromResource];
        groupMessage[@"topicId"] = [NSString stringWithFormat:@"%lld",[msg getTopicId]];
        groupMessage[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
        [list addObject:groupMessage];
    }
    [self sendEventWithName:onHandleMIMCGroupMessage body:list];
}

//处理私聊消息
- (void)onHandleMessage:(NSArray<MIMCMessage *> *)packets user:(MCUser *)user { 
    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (MIMCMessage *msg in packets) {
        NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
        message[@"packetId"] = [msg getPacketId];
        message[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
        message[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
        message[@"fromAccount"] = [msg getFromAccount];
        message[@"fromResource"] = [msg getFromResource];
        message[@"toAccount"] = [msg getToAccount];
        message[@"toResource"] = [msg getToResource];
        message[@"convIndex"] = [NSString stringWithFormat:@"%lld",[msg getConvIndex]];
        message[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
        [list addObject:message];
    }
    [self sendEventWithName:onHandleMIMCMessage body:list];
}

//处理在线消息
- (void)onHandleOnlineMessage:(NSArray<MIMCMessage *> *)packets { 
    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (MIMCMessage *msg in packets) {
        NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
        message[@"packetId"] = [msg getPacketId];
        message[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
        message[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
        message[@"fromAccount"] = [msg getFromAccount];
        message[@"fromResource"] = [msg getFromResource];
        message[@"toAccount"] = [msg getToAccount];
        message[@"toResource"] = [msg getToResource];
        message[@"convIndex"] = [NSString stringWithFormat:@"%lld",[msg getConvIndex]];
        message[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
        [list addObject:message];
    }
    [self sendEventWithName:onHandleMIMCOnlineMessage body:list];
}


- (void)onHandleOnlineMessageAck:(MCOnlineMessageAck *)serverAck { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"packetId"] = [serverAck getPacketId];
    dic[@"code"] = [[NSNumber alloc] initWithInt: [serverAck getCode]];
    dic[@"desc"] = [serverAck getDesc];
    [self sendEventWithName:onHandleMIMCOnlineMessageAck body:dic];
}

//处理发送群聊超时
- (void)onHandleSendGroupMessageTimeout:(MIMCGroupMessage *)msg{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"packetId"] = [msg getPacketId];
    dic[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
    dic[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
    dic[@"fromAccount"] = [msg getFromAccount];
    dic[@"fromResource"] = [msg getFromResource];
    dic[@"topicId"] = [NSString stringWithFormat:@"%lld",[msg getTopicId]];
    dic[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
    [self sendEventWithName:onHandleMIMCSendGroupMessageTimeout body:dic];
}

//处理发送私聊超时
- (void)onHandleSendMessageTimeout:(MIMCMessage *)msg {
    NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
    message[@"packetId"] = [msg getPacketId];
    message[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
    message[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
    message[@"fromAccount"] = [msg getFromAccount];
    message[@"fromResource"] = [msg getFromResource];
    message[@"toAccount"] = [msg getToAccount];
    message[@"toResource"] = [msg getToResource];
    message[@"convIndex"] = [NSString stringWithFormat:@"%lld",[msg getConvIndex]];
    message[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
    [self sendEventWithName:onHandleMIMCSendMessageTimeout body:message];
}

//处理大群聊超时
- (void)onHandleSendUnlimitedGroupMessageTimeout:(MIMCGroupMessage *)msg {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"packetId"] = [msg getPacketId];
    dic[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
    dic[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
    dic[@"fromAccount"] = [msg getFromAccount];
    dic[@"fromResource"] = [msg getFromResource];
    dic[@"topicId"] = [NSString stringWithFormat:@"%lld",[msg getTopicId]];
    dic[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
    [self sendEventWithName:onHandleMIMCSendUnlimitedGroupMessageTimeout body:dic];
}

- (void)onHandleServerAck:(MIMCServerAck *)serverAck { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"packetId"] = [serverAck getPacketId];
    dic[@"code"] = [[NSNumber alloc] initWithInt: [serverAck getCode]];
    dic[@"desc"] = [serverAck getDesc];
    dic[@"convIndex"] = [NSString stringWithFormat:@"%lld",[serverAck getConvIndex]];
    dic[@"sequence"] = [NSString stringWithFormat:@"%lld",[serverAck getSequence]];
    dic[@"timestamp"] = [NSString stringWithFormat:@"%lld",[serverAck getTimestamp]];
    [self sendEventWithName:onHandleMIMCServerAck body:dic];
}


//处理无限大群消息
- (void)onHandleUnlimitedGroupMessage:(NSArray<MIMCGroupMessage *> *)packets { 
    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (MIMCGroupMessage *msg in packets) {
        NSMutableDictionary *groupMessage = [[NSMutableDictionary alloc] init];
        groupMessage[@"packetId"] = [msg getPacketId];
        groupMessage[@"sequence"] = [NSString stringWithFormat:@"%lld",[msg getSequence]];
        groupMessage[@"timestamp"] = [NSString stringWithFormat:@"%lld",[msg getTimestamp]];
        groupMessage[@"fromAccount"] = [msg getFromAccount];
        groupMessage[@"fromResource"] = [msg getFromResource];
        groupMessage[@"topicId"] = [NSString stringWithFormat:@"%lld",[msg getTopicId]];
        groupMessage[@"payload"] = [[NSString alloc] initWithData:[msg getPayload] encoding:NSUTF8StringEncoding];
        [list addObject:groupMessage];
    }
    [self sendEventWithName:onHandleMIMCUnlimitedGroupMessage body:list];
}

- (void)onPullNotification { 
    
}

//处理登录状态变化
- (void)onHandleStatusChanged:(MCUser *)user status:(int)status type:(NSString *)type reason:(NSString *)reason desc:(NSString *)desc { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"type"] = type;
    dic[@"status"] = [[NSNumber alloc] initWithInt:status];
    dic[@"desc"] = desc;
    dic[@"reason"] = reason;
    [self sendEventWithName:onHandleMIMCStatusChanged body:dic];
}

//创建无限大群成功
- (void)onHandleCreateUnlimitedGroup:(int64_t)topicId topicName:(NSString *)topicName code:(int)code desc:(NSString *)desc context:(id)context { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"topicId"] = [[NSNumber alloc] initWithLong:topicId];
    dic[@"topicName"] = topicName;
    dic[@"code"] = [[NSNumber alloc] initWithInt:code];
    dic[@"desc"] = desc;
    [self sendEventWithName:onHandleMIMCCreateUnlimitedGroup body:dic];
}

//取消无限大群成功
- (void)onHandleDismissUnlimitedGroup:(int64_t)topicId { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"topicId"] = [[NSNumber alloc] initWithLong:topicId];
    dic[@"code"] = [[NSNumber alloc] initWithInt:500];
    dic[@"desc"] = @"onHandleDismissUnlimitedGroup success";
    [self sendEventWithName:onHandleMIMCDismissUnlimitedGroup body:dic];
}

//取消无限大群成功
- (void)onHandleDismissUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"topicId"] = [[NSNumber alloc] initWithLong:topicId];
    dic[@"code"] = [[NSNumber alloc] initWithInt:code];
    dic[@"desc"] = desc;
    [self sendEventWithName:onHandleMIMCDismissUnlimitedGroup body:dic];
}

//加入无限大群成功
- (void)onHandleJoinUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"topicId"] = [[NSNumber alloc] initWithLong:topicId];
    dic[@"code"] = [[NSNumber alloc] initWithInt:code];
    dic[@"desc"] = desc;
    [self sendEventWithName:onHandleMIMCJoinUnlimitedGroup body:dic];
}

//退出无限大群成功
- (void)onHandleQuitUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context { 
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"topicId"] = [[NSNumber alloc] initWithLong:topicId];
    dic[@"code"] = [[NSNumber alloc] initWithInt:code];
    dic[@"desc"] = desc;
    [self sendEventWithName:onHandleMIMCQuitUnlimitedGroup body:dic];
}


- (void)onAnswered:(int64_t)callId accepted:(BOOL)accepted desc:(NSString *)desc {
    
}

- (void)onClosed:(int64_t)callId desc:(NSString *)desc {
    
}

- (void)onData:(int64_t)callId fromAccount:(NSString *)fromAccount resource:(NSString *)resource data:(NSData *)data dataType:(RtsDataType)dataType channelType:(RtsChannelType)channelType {
    
}

@end


